describe('LM Frontend E2E', () => {
  beforeEach(() => {
    cy.intercept('GET', '**/api/stats*', { fixture: 'stats.json' }).as('getStats')
    cy.intercept('GET', '**/api/categories', { fixture: 'categories.json' }).as('getCats')
    cy.intercept('GET', '**/api/stats/top-examples*', { fixture: 'top-examples.json' }).as('getTop')
    cy.on('window:before:load', (win) => {
      // Force fallback to polling instead of SSE
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      delete (win as any).EventSource
    })
  })

  it('页面加载与跳转，饼图渲染', () => {
    cy.visit('/')
    cy.contains('进入分析').click()
    cy.url().should('include', '/analysis')
    cy.contains('分类占比')
    cy.wait('@getStats')
    cy.get('canvas').should('exist')
  })

  it('表格联动：点击高亮行', () => {
    cy.visit('/analysis')
    cy.wait('@getStats')
    cy.get('tbody tr').first().click().should('have.class', 'bg-blue-50')
  })

  it('打开批处理抽屉并显示进度(轮询)', () => {
    cy.intercept('GET', '**/api/classify/task/*', { fixture: 'task.json' }).as('getTask')
    cy.visit('/analysis')
    cy.contains('批处理').click()
    cy.contains('批量分类')
    cy.wait('@getTask')
    cy.contains('Processed')
    cy.contains('Success')
    cy.contains('Failed')
  })

  it('导出 CSV 可点击（拦截下载）', () => {
    cy.visit('/analysis')
    cy.window().then((win) => {
      cy.stub(win.URL, 'createObjectURL').returns('blob:mock').as('createUrl')
      // Spy click on anchors
      cy.stub(win.HTMLAnchorElement.prototype, 'click').as('aClick')
    })
    cy.contains('导出 CSV').click()
    cy.get('@createUrl').should('have.been.called')
    cy.get('@aClick').should('have.been.called')
  })

  it('暗黑模式切换', () => {
    cy.visit('/')
    cy.get('.ant-switch').click()
    cy.document().its('documentElement.className').should('contain', 'dark')
  })
})

